package com.kc.gb.device.configurations;

import com.kc.gb.device.common.properties.DeviceProperties;
import com.kc.gb.device.common.utils.I18NHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

@Configuration
public class I18NConfiguration {

    @Resource
    DeviceProperties deviceProperties;

    @Bean
    public ResourceBundleMessageSource messageSource() {
        // 设置系统默认的语言
        String language = deviceProperties.getLanguage();
        if(StringUtils.isEmpty(language)){
            language = Locale.SIMPLIFIED_CHINESE.toLanguageTag();
        }
        Locale locale = Locale.forLanguageTag(language);
        I18NHelper.setDefaultLocale(locale);

        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setDefaultLocale(locale);
        messageSource.setBasename(I18NHelper.I18N_BASE_NAME);
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
        messageSource.setFallbackToSystemLocale(false); // 禁用系统 Locale 回退
        return messageSource;
    }
}
